package jiang.ThreadPool;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName TestThreadPoolFactory
 * @Description: 通用线程工厂
 * @Author jiangshitao
 * @Date 2023/1/28
 */
public class TestThreadPoolFactory implements ThreadFactory {

    /**
     * 这个是递增原子数，用于组成线程的名字
     */
    private AtomicInteger threadIdx = new AtomicInteger(1);

    /**
     * 线程名的前缀
     */
    private String threadNamePrefix;

    /**
     * 构造函数，用于接收前缀
     * @param prefix 前缀
     */
    public TestThreadPoolFactory(String prefix) {
        threadNamePrefix = prefix;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName(threadNamePrefix + "-xxljob-" + threadIdx.getAndIncrement());
        return thread;
    }

}
